//
//  CNImageBrowserSheetView.m
//  CsdnPlus
//
//  Created by 孟瑞瑞 on 2019/3/22.
//  Copyright © 2019 CSDN. All rights reserved.
//

#import "CNImageBrowserSheetView.h"
#import <YBImageBrowser/YBIBCopywriter.h>
#import <YBImageBrowser/YBIBUtilities.h>
#import <CNBaseTool/CNCategoryHeader.h>

//#import "YBImageBrowserTipView.h"


@interface CNImageBrowserSheetCell : UITableViewCell
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *line;
@end
@implementation CNImageBrowserSheetCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#3D3D3D"];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
        }];
    }
    return self;
}
@end


static NSString * const kIdentityOfYBImageBrowserSheetCell = @"kIdentityOfYBImageBrowserSheetCell";
static CGFloat kOffsetSpace = 5;

@interface CNImageBrowserSheetView () <UITableViewDelegate, UITableViewDataSource> {
    CGRect _tableShowFrame;
    CGRect _tableHideFrame;
//    id<YBImageBrowserCellDataProtocol> _data;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *footer;
@end

@implementation CNImageBrowserSheetView

#pragma mark - life cycle

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _cellHeight = 44;
        _cancelText = [YBIBCopywriter sharedCopywriter].cancel;
        _maxHeightScale = 0.7;
        _showDuration = 0.2;
        _hideDuration = 0.1;
        _backAlpha = 0.3;

        _actions = [NSMutableArray array];

        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - public

- (void)showToView:(UIView *)view orientation:(UIDeviceOrientation)orientation {
    if (self.actions.count == 0) return;
    
    [view addSubview:self];
    self.frame = view.bounds;
    
    UIEdgeInsets padding = YBIBPaddingByBrowserOrientation(orientation);
    
    CGFloat footerHeight = padding.bottom;
    CGFloat tableHeight = self.cellHeight * (self.actions.count + 1) + kOffsetSpace + footerHeight;
    
    _tableShowFrame = self.frame;
    _tableShowFrame.size.height = MIN(self.maxHeightScale * self.bounds.size.height, tableHeight);
    _tableShowFrame.origin.y = self.bounds.size.height - _tableShowFrame.size.height;
    
    _tableHideFrame = _tableShowFrame;
    _tableHideFrame.origin.y = self.bounds.size.height;
    
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    self.tableView.frame = _tableHideFrame;
    self.tableView.tableFooterView.bounds = CGRectMake(0, 0, self.tableView.frame.size.width, footerHeight);
    [UIView animateWithDuration:self.showDuration animations:^{
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:self->_backAlpha];
        self.tableView.frame = self->_tableShowFrame;
    }];
}

- (void)hideWithAnimation:(BOOL)animation {
    if (!self.superview) return;
    
    void(^animationsBlock)(void) = ^{
        self.tableView.frame = self->_tableHideFrame;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
    };
    void(^completionBlock)(BOOL n) = ^(BOOL n){
        [self removeFromSuperview];
    };
    if (animation) {
        [UIView animateWithDuration:self.hideDuration animations:animationsBlock completion:completionBlock];
    } else {
        animationsBlock();
        completionBlock(NO);
    }
}

//
//#pragma mark - <YBImageBrowserSheetViewProtocol>
//
//- (void)yb_browserShowSheetViewWithData:(id<YBImageBrowserCellDataProtocol>)data layoutDirection:(YBImageBrowserLayoutDirection)layoutDirection containerSize:(CGSize)containerSize {
//    if (self.actions.count <= 0) return;
//    
//    self->_data = data;
//    
//    CGFloat width = containerSize.width, height = containerSize.height;
//    
//    CGFloat tableViewHeight = MIN(self.heightOfCell * self.actions.count + self.heightOfCell + 5 + YBIB_HEIGHT_EXTRABOTTOM, height * self.maxHeightScale);
//    self->_hideFrame = CGRectMake(0, height, width, tableViewHeight);
//    self->_showFrame = CGRectMake(0, height - tableViewHeight, width, tableViewHeight);
//    
//    self.frame = CGRectMake(0, 0, width, height);
//    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
//    self.tableView.frame = self->_hideFrame;
//    [self.tableView reloadData];
//    self.footer.frame = CGRectMake(0, 0, width, YBIB_HEIGHT_EXTRABOTTOM);
//    [UIView animateWithDuration:self->_animateDuration animations:^{
//        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.35];
//        self.tableView.frame = self->_showFrame;
//    }];
//}
//
//- (void)yb_browserHideSheetViewWithAnimation:(BOOL)animation {
//    if (!self.superview) return;
//    
//    void(^animationsBlock)(void) = ^{
//        self.tableView.frame = self->_hideFrame;
//        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
//    };
//    void(^completionBlock)(BOOL n) = ^(BOOL n){
//        [self removeFromSuperview];
//    };
//    if (animation) {
//        [UIView animateWithDuration:self->_animateDuration animations:animationsBlock completion:completionBlock];
//    } else {
//        animationsBlock();
//        completionBlock(NO);
//    }
//}
//
//- (NSInteger)yb_browserActionsCount {
//    return self.actions.count;
//}
//
#pragma mark - <UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0)
        return self.actions.count;
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.cellHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0)
        return 0.001;
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CNImageBrowserSheetCell *cell = [tableView dequeueReusableCellWithIdentifier:kIdentityOfYBImageBrowserSheetCell];
    if (indexPath.section == 0) {
        cell.line.hidden = YES;
        YBIBSheetAction *action = self.actions[indexPath.row];
        cell.titleLabel.text = action.name;
        cell.backgroundColor = [UIColor colorWithHexString:@"#f0f0f5"];
    } else {
        cell.line.hidden = YES;
        cell.titleLabel.text = self.cancelText;
        cell.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
    }
    return cell;
}

#pragma mark - <UITableViewDelegate>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        YBIBSheetAction *action = self.actions[indexPath.row];
        if (action.action) action.action(self.currentdata());
    } else {
        [self hideWithAnimation:YES];
    }
}

#pragma mark - touch

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [touches.anyObject locationInView:self];
    if (!CGRectContainsPoint(self.tableView.frame, point)) {
        [self hideWithAnimation:YES];
    }
}

#pragma mark - getter

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 44;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.backgroundColor = [UIColor whiteColor];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.alwaysBounceVertical = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        //_tableView.tableFooterView = self.footer;
        [_tableView registerClass:CNImageBrowserSheetCell.class forCellReuseIdentifier:kIdentityOfYBImageBrowserSheetCell];
    }
    return _tableView;
}

- (UIView *)footer {
    if (!_footer) {
        _footer = [UIView new];
        //_footer.backgroundColor = [UIColor whiteColor];
    }
    return _footer;
}

@end
